package com.hengpeng.itfin.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResultFactory;
import com.hengpeng.itfin.exception.TransException;
import com.hengpeng.itfinbase.message.ResponseMessage;

public class MessageUtil
{
	private final static Log logger = LogFactory.getLog(MessageUtil.class);

	/**
	 * <pre>
	 * 根据错误码
	 * </pre>
	 * @param code
	 * @return
	 */
	public static <T extends ResponseMessage> T getErrorResponseMessage(Class<T> clz, String code)
	{
		String message = ServiceResultFactory.getRspMessage(code);
		return getErrorResponseMessage(clz, code, message);
	}

	public static <T extends ResponseMessage> T getErrorResponse(Class<T> clz, String message)
	{
		return getErrorResponseMessage(clz, Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, message);
	}

	/**
	 * <pre>
	 * 根据错误码返回错误对象
	 * </pre>
	 * @param <T>
	 * @param code
	 * @param message
	 * @param clz
	 * @return
	 */
	public static <T extends ResponseMessage> T getErrorResponseMessage(Class<T> clz, String code, String message)
	{
		T response = null;
		try
		{
			response = clz.newInstance();
			response.setRespCode(code);
			response.setRespDesc(message);
		}
		catch (Exception e)
		{
			logger.error("动态实例化{" + clz.getName() + "}出现异常", e);
			throw new TransException("动态实例化{" + clz.getName() + "}出现异常");
		}
		return response;
	}

	public static ResponseMessage GetSuccessMessage()
	{
		return new ResponseMessage();
	}
}
